package demo1;

import java.util.List;

public class SingleList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;

    //头插法
    public void addFirst(int data) {
        ListNode node  = new ListNode(data);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            return;
        }

        //遍历链表找尾
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }

        cur.next = node;
    }

    private void checkIndex(int index) throws IndexOutOfBoundException {
        if(index < 0 || index >size()) {
            throw new IndexOutOfBoundException("下标越界！！！");
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        try {
            checkIndex(index);
            if(index == 0) {
                addFirst(data);
                return;
            }


            ListNode node  = new ListNode(data);
            ListNode cur = head;
            ListNode prev = cur;
            for(int i = 0; i < index; i++) {
                prev = cur;
                cur = cur.next;
            }
            node.next = cur;
            prev.next = node;
        } catch (IndexOutOfBoundException e) {
            System.out.println("下标越界！！！");
            e.printStackTrace();
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
        }

        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        if(head == null) {
            return;
        }

        if(head.val == key) {
            head = head.next;
            return;
        }

        ListNode cur = findFrontNodeOfKey(key);
        ListNode del = cur.next;

        cur.next = del.next;
    }

    //无法处理头节点
    private ListNode findFrontNodeOfKey(int key) {
        ListNode cur = head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(head == null) {
            return;
        }

        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
            } else {
                prev = cur;
            }
            cur = cur.next;
        }

        if(head.val == key) {
            head = head.next;
        }

    }

    //得到单链表的长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode tmp = cur.next;
            cur.next = null;
            cur = tmp;
        }
        head =null;
    }
}
